package domain.entities;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Budget entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see Budget
 * @author MyEclipse Persistence Tools
 */

public class BudgetDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(BudgetDAO.class);
	// property constants
	public static final String AMOUNT_SBWINITIAL = "amountSbwinitial";
	public static final String AMOUNT_SBWREVISED = "amountSbwrevised";
	public static final String AMOUNT_SBWFINAL = "amountSbwfinal";

	protected void initDao() {
		// do nothing
	}

	public void save(Budget transientInstance) {
		log.debug("saving Budget instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Budget persistentInstance) {
		log.debug("deleting Budget instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Budget findById(Integer id) {
		log.debug("getting Budget instance with id: " + id);
		try {
			Budget instance = (Budget) getHibernateTemplate().get(
					"domain.entities.Budget", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Budget> findByExample(Budget instance) {
		log.debug("finding Budget instance by example");
		try {
			List<Budget> results = (List<Budget>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Budget instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Budget as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Budget> findByAmountSbwinitial(Object amountSbwinitial) {
		return findByProperty(AMOUNT_SBWINITIAL, amountSbwinitial);
	}

	public List<Budget> findByAmountSbwrevised(Object amountSbwrevised) {
		return findByProperty(AMOUNT_SBWREVISED, amountSbwrevised);
	}

	public List<Budget> findByAmountSbwfinal(Object amountSbwfinal) {
		return findByProperty(AMOUNT_SBWFINAL, amountSbwfinal);
	}

	public List findAll() {
		log.debug("finding all Budget instances");
		try {
			String queryString = "from Budget";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Budget merge(Budget detachedInstance) {
		log.debug("merging Budget instance");
		try {
			Budget result = (Budget) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Budget instance) {
		log.debug("attaching dirty Budget instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Budget instance) {
		log.debug("attaching clean Budget instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static BudgetDAO getFromApplicationContext(ApplicationContext ctx) {
		return (BudgetDAO) ctx.getBean("BudgetDAO");
	}

    public List<Budget> findByProtocolo(Protocolo protocolo){
        List<Budget> result = getSession().createCriteria(Budget.class)
                .createAlias("protocoloCompromisopais", "pcp", CriteriaSpecification.INNER_JOIN)
                .createAlias("pcp.compromisopais", "cp", CriteriaSpecification.INNER_JOIN)
                .add(Restrictions.eq("pcp.protocolo", protocolo))
                .addOrder(Order.asc("cp.country"))
                .list();
        return result;
    }

    public Budget findByCompromisoPais(Integer countryId){
        List<Budget> result = getSession().createCriteria(Budget.class)
                .createAlias("protocoloCompromisopais", "pcp", CriteriaSpecification.INNER_JOIN)
                .createAlias("pcp.compromisopais", "cp", CriteriaSpecification.INNER_JOIN)
                .add(Restrictions.eq("cp.id", countryId))
                .list();
        if(result.size() == 0)
            return null;
        else
            return (Budget)(result.get(0));
    }
}